var mainApp = new Vue({
  el: '#mainApp',
  data: function () {
    return {
      categoryData: _CATEGORES_,
      filter: {
        categoryFirst: {
          value: '',
          val: '',
          show: false,
          children: _CATEGORES_,
          old: null
        },
        categorySecond: {
          value: '',
          val: '',
          show: false,
          children: [],
          old: null
        },
        certNo: '',
        brand: '',
        manuName: '',
        proType: '',
        certEdateStart: '',
        certEdateEnd: '',
        dateRange: []
      },
      dataTotal: 0,
      resultList: [],
      dataCache: {},
      searchParams: {},
      seatched: false,
      loaded: true,
      currentPage: 1,
      step: 1,
      layerContacts: {
        show: false,
        tab: 1,
        contacts: [],
        orgs: _ORGS_
      },
      xmlb: ''
    }
  },
  methods: {
    clearForm: function () {
      let self = this, filter = self.filter;
      filter.categoryFirst = {
        value: '',
        val: '',
        show: false,
        children: _CATEGORES_,
        old: null
      };
      filter.categorySecond = {
        value: '',
        val: '',
        show: false,
        children: [],
        old: null
      }
      filter.certNo = '';
      filter.brand = '';
      filter.manuName = '';
      filter.proType = '';
      filter.certEdateStart = '';
      filter.certEdateEnd = '';
      filter.dateRange = [];
    },
    initResultList: function (step) {
      var self = this;
      if (!!self.dataCache['page-'+self.currentPage]) {
        self.resultList = self.dataCache['page-'+self.currentPage];
      }
    },
    doInputTextFilter: function (key) {
      var self = this, f = self.filter;
      if (key === 'proType') {
        return f[key].replace(/\^|\%|\￥|\<|\>|\《|\》/g, '');
      } else {
        return f[key].replace(/\*|\^|\%|\&|\￥|\#|\@|\<|\>|\《|\》/g, '');
      }
    },
    doSearch: function() {
      var self = this, filter = self.filter;
      if ((filter.categoryFirst.val && filter.categoryFirst.val!=='0') || (filter.categorySecond.val && filter.categorySecond.val!=='0') || self.doInputTextFilter('proType') || self.doInputTextFilter('certNo') || self.doInputTextFilter('brand') || self.doInputTextFilter('manuName') || filter.certEdateStart || filter.certEdateEnd) {
        self.searchParams = {
          xmlb: filter.categoryFirst.val,
          items: filter.categorySecond.val,
          certNo: self.doInputTextFilter('certNo'),
          brand: self.doInputTextFilter('brand'),
          manuName: self.doInputTextFilter('manuName'),
          proType: self.doInputTextFilter('proType'),
          certEdateStart: filter.certEdateStart,
          certEdateEnd: filter.certEdateEnd,
          startIndex: 1,
          endIndex: 150,
          buttonName: 'queryBtnPC'
        }
        self.dataCache = {};
        self.step = 1;
        self.dataTotal = 0;
        self.resultList = [];
        self.currentPage = 1;
        self.getDataList();
      } else {
        system.message.light('请至少输入一个查询条件，查询条件不得包含特殊字符！');
      }
    },
    getDataList: function () {
      var self = this;
      if (!!self.loaded) {
        if (typeof self.dataCache['page-'+self.currentPage] === 'object') {
          self.step = 1;
          self.resultList = [];
          self.initResultList(self.step);
        } else {
          self.loaded = false;
          system.ajax({
            url: '/dir_local/dist/queryQdData!queryQd.action',
            type: 'post',
            data: self.searchParams,
            success: function (res) {
              if (res['code'] === 0) {
                self.dataCache['page-'+self.currentPage] = res['data'];
                self.initResultList(self.step);
                //self.resultList = res['data'];
                self.dataTotal = res['size'];
                //self.renderLayPage();
              } else {
                system.message.light(res['message'] || '获取数据错误，请重试');
              }
            },
            complete: function () {
              self.seatched = true;
              self.loaded = true;
            }
          });
        }
      }
    },
    queryIng: function(e, t) {
      e = e.target;
      var self = this, temp = t.old.children, res= {};
      temp = temp.filter(function(p) {
        return p.text.indexOf(e.value) !== -1;
      });
      t.children = temp;
    },
    queryEnd: function(t) {
      if (!t.val) {
        t.value = t.old.w;
        t.val = t.old.v;
        t.children = t.old.children;
      }
    },
    queryStart: function(t) {
      t.val = '';
      t.old = {
        w: t.value,
        v: t.val,
        children: t.children
      }
    },
    renderLayPage: function (cur) {
      var self = this;
      self.searchParams.startIndex = cur * 150 - 149;
      self.searchParams.endIndex = cur * 150;
      self.searchParams.buttonName = 'nextPagePC';
      self.step = 1;
      self.currentPage = cur
      self.resultList = [];
      self.getDataList();
    },
    setCateGory: function(target, v, m) {
      var self = this;
      target.value = v.text;
      if (typeof m === 'object' && target.val !== v) {
        m.children = v.children;
        m.value = '';
        m.val = '';
      }
      target.val = v.values;
      target.show = false;
    },
    showSelectList: function (e, target) {
      if (!target.show && e.target.nodeName !== 'A') {
        this.filter.categoryFirst.show = false;
        this.filter.categorySecond.show = false;
        target.show = true;
        this.queryStart(target);
      }
    },
    getContactInfo: function(certNo) {
      var self = this;
      self.layerContacts.contacts = [];
      system.ajax({
        url: '/dir_local/dist/queryQdData!queryLink.action',
        type: 'post',
        data: {
          certNo: certNo,
          buttonName: 'queryLinkPC'
        },
        success: function(res) {
          if (res['code'] === 0) {
            res = res['data'];
            self.layerContacts.contacts = res['fields'];
            self.$nextTick(function(){
              self.changeLayerTab(1);
              self.showLayer(true);
            })
          } else {
            system.message.light(res['message'] || '查询联系人失败，请重试');
          }
        }
      })
    },
    showLayer: function (s) {
      this.layerContacts.show = s;
      if (!!s) {
        document.body.style.overflowY = 'hidden';
      } else {
        document.body.style.overflowY = 'auto';
      }
    },
    changeLayerTab: function (tab) {
      this.layerContacts.tab = tab;
    },
    categoryFirstChange: function (v) {
      var self = this, cf = self.filter.categoryFirst;
      self.filter.categorySecond.children = [];
      self.filter.categorySecond.value = '';
      self.filter.categorySecond.val = '';
      for (var i = 0; i < cf.children.length; i++) {
        if (cf.children[i].values === v) {
          self.filter.categorySecond.children = cf.children[i].children;
          break;
        }
      }
      cf.val = v;
    },
    categorySecondChange: function (v) {
      this.filter.categorySecond.val = v;
    }
  }
});
$(function(){
  $(document).on('mousedown', function(e) {
    e = $(e.target);
    if (!e.hasClass('search-select') && !e.closest('.search-select').length) {
      mainApp.filter.categoryFirst.show = false;
      mainApp.filter.categorySecond.show = false;
    }
  });
  var startDate = laydate.render({
    elem: '#actionDateStart'
    ,isPreview: false
    ,format: 'yyyy-MM-dd'
    ,btns: ['clear', 'confirm']
    ,isInitValue: true
    ,min: 0
    ,max: 365
    ,done: function (value) {
      if (!!mainApp.filter.certEdateStart) {
        var v = value.split('-'), _start = new Date(v[0], parseInt(v[1])+1, v[2], 0, 0, 0).getTime(), s = mainApp.filter.certEdateEnd.split('-'), _end = new Date(s[0], parseInt(s[1])+1, s[2], 0, 0, 0).getTime();
        if (_end < _start) {
          system.message.light('结束时间不得小于开始时间');
          setTimeout(function () {
            $('#actionDateStart').val('');
            mainApp.filter.certEdateStart = '';
          }, 20);
        } else {
          mainApp.filter.certEdateStart = value;
        }
      } else {
        mainApp.filter.certEdateStart = value;
      }
    }
  });
  var endDate = laydate.render({
    elem: '#actionDateEnd'
    ,isPreview: false
    ,format: 'yyyy-MM-dd'
    ,btns: ['clear', 'confirm']
    ,isInitValue: true
    ,min: 0
    ,done: function (value) {
      if (!!mainApp.filter.certEdateStart) {
        var v = value.split('-'), _end = new Date(v[0], parseInt(v[1])+1, v[2], 0, 0, 0).getTime(), s = mainApp.filter.certEdateStart.split('-'), _start = new Date(s[0], parseInt(s[1])+1, s[2], 0, 0, 0).getTime();
        if (_end < _start) {
          system.message.light('结束时间不得小于开始时间');
          setTimeout(function () {
            $('#actionDateEnd').val('');
            mainApp.filter.certEdateEnd = '';
          }, 20);
        } else {
          mainApp.filter.certEdateEnd = value;
        }
      } else {
        mainApp.filter.certEdateEnd = value;
      }
    }
  });

  //返回顶部
    $("#to_top").hide();
    $(window).scroll(function () {
      if ($(window).scrollTop() > 100) {
        $("#to_top").fadeIn(100);
      } else {
        $("#to_top").fadeOut(100);
      }
    });
    $("#to_top").click(function () {
      $('body,html').animate({scrollTop: 0});
      return false;
    });
});
